package com.yimils.core.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yimils.core.common.dto.GetQueryParams;
import com.yimils.core.common.dto.RspBase;
import com.yimils.core.common.dto.SysCode;
import com.yimils.core.sys.dto.ReqAddUser;
import com.yimils.core.sys.dto.ReqUpdateUser;
import com.yimils.core.sys.service.UserRoleService;
import com.yimils.core.sys.service.UserService;
import com.yimils.core.sys.vo.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class UserController {

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "userRoleService")
    private UserRoleService userRoleService;

    /**
     * showdoc
     *
     * @param username   必选 String 用户名
     * @param nickname   必选 String 用户昵称
     * @param password   必选 Long 密码
     * @param roleId     必选 Long 角色id
     * @param superiorId 可选 Long 上级用户id
     * @param deptMainId 必选 Long 主部门ID
     * @param deptSubId  可选 Long 子部门ID
     * @return {"code":0,"msg":"创建用户成功"}
     * @catalog 系统管理/用户管理
     * @title 添加用户
     * @description 创建用户, 前端密码传参须MD5加密
     * @method post
     * @url /user
     * @json_param {"username":"testadmin","nickname":"超级管理","password":"5050c06487a946899ea12ef74a09289a","roleId":1,"superiorId":3,"deptMainId":1,"deptSubId":2}
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 暂无
     * @number 1
     */
    @PostMapping("/user")
    public RspBase createUser(@RequestBody ReqAddUser reqParams) {
        if (userService.createUser(reqParams)) {
            return RspBase.OK("操作成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "创建用户失败");
    }

    /*@PutMapping("/user")
    public RspBase updateUser(@RequestBody UserVo userVo) {
        if (userService.updateUserVo(userVo)) {
            return RspBase.OK("操作成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "操作失败");
    }*/

    @PutMapping("/user")
    public RspBase updateUser(@RequestBody ReqUpdateUser dto) {
        if (userService.updateUser(dto)) {
            return RspBase.OK("操作成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "操作失败");
    }

    /**
     * showdoc
     *
     * @param pageNo   必选 String 页号
     * @param pageSize 必选 Long 页量
     * @return {"code":0,"msg":"","data":{"records":[{"id":1,"name":"admin","createTime":"2019-09-12T10:42:01","updateTime":"2019-09-12T10:42:01"},{"id":9,"name":"testboss","createTime":"2019-09-18T17:22:08","updateTime":"2019-09-18T17:22:08"},{"id":10,"name":"testseo","createTime":"2019-09-19T10:13:19","updateTime":"2019-09-19T10:13:19"}],"total":3,"size":10,"current":1,"searchCount":true,"pages":1}}
     * @catalog 系统管理/用户管理
     * @title 获取用户表
     * @description 获取所有用户
     * @method get
     * @url /users
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 4
     */
    @GetMapping("/users")
    public RspBase getUsers(GetQueryParams pageParam) {
        RspBase rsp = RspBase.OK();
        IPage<UserVo> userList = userService.getUsers(pageParam);
        rsp.put("data", userList);
        return rsp;
    }

    @GetMapping("/user/{id}")
    public RspBase getUserById(@PathVariable Long id) {
        RspBase rsp = RspBase.OK();
        rsp.put("userInfo", userService.getById(id));
        return rsp;
    }
    /**
     * 获取团队
     * @param queryParam
     * @return
     */
    @GetMapping("/user/team")
    public RspBase getUserTeam(GetQueryParams queryParam) {
        RspBase rsp = RspBase.OK();
        IPage<TeamVo> teamList = userService.getUserTeam(queryParam);
        rsp.put("data", teamList);
        return rsp;
    }
    /**
     * 获取合伙人列表
     * @param queryParam
     * @return
     */
    @GetMapping("/user/partners")
    public RspBase getPartners(GetQueryParams queryParam) {
        RspBase rsp = RspBase.OK();
        IPage<PartnerVo> userList = userService.getPartner(queryParam);
        rsp.put("data", userList);
        return rsp;
    }

    /**
     * 获取酒店负责人列表
     * @param queryParam
     * @return
     */
    @GetMapping("/user/hotels")
    public RspBase getHotels(GetQueryParams queryParam) {
        RspBase rsp = RspBase.OK();
        IPage<HotelVo> teamList = userService.getUserHotel(queryParam);
        rsp.put("data", teamList);
        return rsp;
    }

    /**
     * 获取酒店负责人列表
     * @param queryParam
     * @return
     */
    @GetMapping("/user/feeders")
    public RspBase getFeeders(GetQueryParams queryParam) {
        RspBase rsp = RspBase.OK();
        IPage<FeederVo> userList = userService.getFeeders(queryParam);
        rsp.put("data", userList);
        return rsp;
    }

    /**
     * 获取运营专员列表
     * @param
     * @return
     */
    @GetMapping("/user/agents")
    public RspBase getAgents(GetQueryParams queryParam) {
        RspBase rsp = RspBase.OK();
        IPage<PartnerAgentVo> userList = userService.getAgents(queryParam);
        rsp.put("data", userList);
        return rsp;
    }
    /**
     * showdoc
     *
     * @param password 可选 String 新密码
     * @param active   可选 Integer 是否启用1启用0禁用
     * @return {"code":0,"msg":""}
     * @catalog 系统管理/用户管理
     * @title 修改用户密码
     * @description 修改指定用户密码
     * @method put
     * @url /user/{id}
     * @json_param {"password":"5050c06487a946899ea12ef74a09289a","active":0,"deptId":1}
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 4
     */
    @PutMapping("/user/{id}")
    public RspBase updateUserPassword(@PathVariable Long id, @RequestBody Map<String, Object> bodyParams) {
        if (bodyParams.get("password") != null) {
            if (userService.updateUserPassword(id, (String) bodyParams.get("password"))) {
                return RspBase.OK("操作成功");
            }
        }

        if (bodyParams.get("active") != null) {
            if (userService.lockUser(id, (Integer) bodyParams.get("active"))) {
                return RspBase.OK("操作成功");
            }
        }

        return RspBase.ERROR(SysCode.OPT_FAILURE, "操作失败");
    }

    /**
     * showdoc
     *
     * @return {"code":0,"msg":""}
     * @catalog 系统管理/用户管理
     * @title 心跳保活
     * @description 修改指定用户密码
     * @method get
     * @url /keepalive
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark
     * @number 4
     */
    @GetMapping("/keepalive")
    public RspBase keepalive() {
        SecurityUtils.getSubject().getSession().touch();
        return RspBase.OK();
    }
}

